//
//  OO_ChooseView.m
//  ShopProject
//
//  Created by macbookpro on 2018/9/30.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "OO_ChooseView.h"
#import "FGNumberStepper.h"
@implementation OO_ChooseView

+(instancetype)loadFromNib{
    OO_ChooseView *views = [[[NSBundle mainBundle] loadNibNamed:[[self class] description] owner:self options:nil] objectAtIndex:0];
    views.frame = UIScreen.mainScreen.bounds;
    
    FGNumberStepper *setpper = [[FGNumberStepper alloc] initWithNumber:0 minmualNumber:0 changeAction:^(FGNumberStepper *s, NSInteger number) {
        views.number = number;

    }];
    setpper.canInput = YES;
    setpper.frame = CGRectMake(0, 0, 100, 30);
    [views.numberView addSubview:setpper];
    return views;
}
-(void)setModel:(OO_GoodsModel *)model
{
    _model = model;
    [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:model.image[0]] placeholderImage:[UIImage imageNamed:IMAGEPLACEHOLDER]];
    self.nameLable.text = model.goodsName;
    self.contentLable.text  = model.describe;
    self.moneyLable.text = [NSString stringWithFormat:@"¥%.2lf",model.price];
    
    
}
- (IBAction)dissmissbuttonpress:(id)sender {
    
    [self removeFromSuperview];
    
}
- (IBAction)sureButtonPress:(UIButton *)sender {

    
    if (self.number == 0) {
        
//
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请添加数量" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
//
//        }];
//
//        [alert addAction:okAction];
//
//        [UIApplication.sharedApplication.delegate.window.rootViewController presentViewController:alert animated:true completion:nil];
        
        [MBProgressHUD showTipMessageInWindow:@"请添加数量"];
        return;
    }
    
    if (self.shopCar) {
        OO_OrderModel *orderModel = [OO_OrderModel new ];
        orderModel.goodsName = self.model.goodsName;
        orderModel.price = [NSString stringWithFormat:@"%.2f",self.model.price];
        orderModel.logionUser = [UserDefaults readFromPlist:LOGIN_STATUS_TEL];
        orderModel.ID = [CommonAPI getOnlyId];
        orderModel.goodsImageUrl = self.model.image[0];
        orderModel.goodsNumber = self.number;
        
        
        WeakSelf
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            
            JQFMDB *db = [JQFMDB shareDatabase];
            [db jq_insertTable:SHOPCARTABLE dicOrModel:orderModel];
            
            dispatch_async(dispatch_get_main_queue(), ^{
//                if (weakSelf.chooseSuccessBlock) {
//                    weakSelf.chooseSuccessBlock(orderModel);
//                }
                [weakSelf removeFromSuperview];
                [MBProgressHUD showTipMessageInWindow:@"添加成功"];
            });
            
            
        });
    }
    else{
    OO_OrderModel *orderModel = [OO_OrderModel new ];
    orderModel.goodsName = self.model.goodsName;
    orderModel.price = [NSString stringWithFormat:@"%.2f",self.model.price];
    orderModel.logionUser = [UserDefaults readFromPlist:LOGIN_STATUS_TEL];
    orderModel.ID = [CommonAPI getOnlyId];
    orderModel.goodsImageUrl = self.model.image[0];
    orderModel.goodsNumber = self.number;
    
    

            if (self.chooseSuccessBlock) {
                self.chooseSuccessBlock(orderModel);
                [self removeFromSuperview];

            }

        
        

    
    }
    
    
    
}
@end
